<?php

namespace common\models\ofc;

use Yii;

/**
 * This is the model class for table "{{%ofc_team_achievement}}".
 *
 * @property int $id
 * @property int $year 年份
 * @property string|null $level 联赛等级
 * @property int $team_id 球队ID
 * @property string|null $team_name 球队名称
 * @property int $won 胜
 * @property int $draw 平
 * @property int $lost 负
 * @property int $credit 积分
 * @property int $rank 排名
 * @property int $status 状态
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 */
class OfcTeamAchievement extends \common\db\ActiveRecord {

    const STATUS_ACTIVE = 10;
    const STATUS_DEFAULT = 0;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%ofc_team_achievement}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['year', 'team_id', 'won', 'draw', 'lost', 'credit', 'rank', 'status', 'created_at', 'updated_at'], 'integer'],
            [['level'], 'string', 'max' => 50],
            [['team_name'], 'string', 'max' => 100],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'year' => '年份',
            'level' => '联赛等级',
            'team_id' => '球队ID',
            'team_name' => '球队名称',
            'won' => '胜',
            'draw' => '平',
            'lost' => '负',
            'credit' => '积分',
            'rank' => '排名',
            'status' => '状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function getTeam() {
        return $this->hasOne(OfcTeam::class, ['id' => 'team_id']);
    }

    /**
     * {@inheritdoc}
     */
    public function fields() {
        return [
            'id',
            'year',
            'level',
            'team_name',
            'won',
            'draw',
            'lost',
            'credit',
            'rank',
        ];
    }

    /**
     * {@inheritdoc}
     */
    public static function findAllDefault() {
        $query = static::find()->alias('t');
        $query->innerJoinWith('team team');
        $query->andWhere([
            't.status' => static::STATUS_ACTIVE,
            'team.is_default' => 1
        ]);
        $query->orderBy([
            'year' => SORT_DESC
        ]);
        return $query->all();
    }

}
